package com.zhang.novel.controller;

import com.github.pagehelper.PageInfo;
import com.zhang.novel.core.bean.ResultBean;
import com.zhang.novel.core.bean.UserDetails;
import com.zhang.novel.core.enums.ResponseStatus;
import com.zhang.novel.entity.BookComment;
import com.zhang.novel.entity.BookContent;
import com.zhang.novel.search.BookSP;
import com.zhang.novel.service.BookService;
import com.zhang.novel.vo.BookSettingVO;
import com.zhang.novel.vo.BookVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "书籍接口")
@RequestMapping("book")
@RequiredArgsConstructor
public class BookController extends BaseController {

    private final BookService bookService;

    private final RabbitTemplate rabbitTemplate;

    @Value("${spring.rabbitmq.enable}")
    private Integer enable;


    /**
     * 查询首页小说设置列表数据
     */
    @ApiOperation("查询首页小说设置列表数据")
    @GetMapping("listBookSetting")
    public ResultBean listBookSetting() {
        return ResultBean.ok(bookService.listBookSettingVO());
    }


    /**
     * 查询首页点击榜单数据
     */
    @GetMapping("listClickRank")
    @ApiOperation("查询首页点击榜单数据")
    public ResultBean listClickRank() {
        return ResultBean.ok(bookService.listClickRank());
    }

    /**
     * 查询首页新书榜单数据
     */
    @GetMapping("listNewRank")
    @ApiOperation("查询首页新书榜单数据")
    public ResultBean listNewRank() {
        return ResultBean.ok(bookService.listNewRank());
    }

    /**
     * 查询首页更新榜单数据
     */
    @GetMapping("listUpdateRank")
    @ApiOperation("查询首页更新榜单数据")
    public ResultBean listUpdateRank() {
        return ResultBean.ok(bookService.listUpdateRank());
    }

    /**
     * 查询小说分类列表
     */
    @GetMapping("listBookCategory")
    @ApiOperation("查询小说分类列表")
    public ResultBean listBookCategory() {
        return ResultBean.ok(bookService.listBookCategory());
    }

    /**
     * 分页搜索
     */
    @PostMapping("searchByPage")
    @ApiOperation("分页搜索")
    public ResultBean searchByPage(BookSP bookSP, @RequestParam(value = "curr", defaultValue = "1") int page, @RequestParam(value = "limit", defaultValue = "20") int pageSize) {
        return ResultBean.ok(bookService.searchByPage(bookSP, page, pageSize));
    }

    /**
     * 查询小说详情信息
     */
    @PostMapping("queryBookDetail")
    @ApiOperation("查询小说详情信息")
    public ResultBean queryBookDetail(Long id) {
        return ResultBean.ok(bookService.queryBookDetail(id));
    }


    /**
     * 查询小说排行信息
     */
    @PostMapping("listRank")
    @ApiOperation("查询小说排行信息")
    public ResultBean listRank(@RequestParam(value = "type", defaultValue = "0") Byte type,
                               @RequestParam(value = "limit", defaultValue = "30") Integer limit) {
        return ResultBean.ok(bookService.listRank(type, limit));
    }

    /**
     * 增加点击次数
     */
    @PostMapping("addVisitCount")
    @ApiOperation("增加点击次数")
    public ResultBean addVisitCount(Long bookId) {
        if (1 == enable) {
            // 启用rabbitMQ
            rabbitTemplate.convertAndSend("ADD-VISIT-COUNT-EXCHANGE","",bookId);
        }else {
            bookService.addVisitCount(bookId, 1);
        }
        return ResultBean.ok();
    }

    /**
     * 查询章节相关信息
     */
    @PostMapping("queryBookIndexAbout")
    @ApiOperation("查询章节相关信息")
    public ResultBean queryBookIndexAbout(Long bookId, Long lastBookIndexId) {
        Map<String, Object> data = new HashMap<>(2);

        data.put("bookIndexCount", bookService.queryIndexCount(bookId));

        BookContent bookContent = bookService.queryBookContent(lastBookIndexId);
        String content = bookContent.getContent();
        if (content.length() > 42) {
            content = content.substring(0, 42);
        }
        data.put("bookAbout", content);
        return ResultBean.ok(data);
    }

    /**
     * 根据分类id查询同类推荐书籍
     */
    @PostMapping("listRecBookByCatId")
    @ApiOperation("根据分类id查询同类推荐书籍")
    public ResultBean listRecBookByCatId(Integer catId) {
        return ResultBean.ok(bookService.listRecBookByCatId(catId));
    }


    /**
     * 分页查询书籍评论列表
     */
    @PostMapping("listCommentByPage")
    @ApiOperation("分页查询书籍评论列表")
    public ResultBean listCommentByPage(@RequestParam("bookId") Long bookId,
                                        @RequestParam(value = "curr", defaultValue = "1") int page,
                                        @RequestParam(value = "limit", defaultValue = "5") int pageSize) {
        return ResultBean.ok(bookService.listCommentByPage(null, bookId, page, pageSize));
    }

    /**
     * 新增评价
     */
    @PostMapping("addBookComment")
    @ApiOperation("新增评价")
    public ResultBean addBookComment(BookComment comment, HttpServletRequest request) {
        UserDetails userDetails = getUserDetails(request);
        if (null == userDetails) {
            return ResultBean.fail(ResponseStatus.NO_LOGIN);
        }
        bookService.addBookComment(userDetails.getId(), comment);
        return ResultBean.ok();
    }

    /**
     * 根据小说ID查询小说前十条最新更新目录集合
     */
    @PostMapping("queryNewIndexList")
    @ApiOperation("根据小说ID查询小说前十条最新更新目录集合")
    public ResultBean queryNewIndexList(Long bookId) {
        return ResultBean.ok(bookService.queryIndexList(bookId, "index_num desc", 10));
    }
}
